home *** CD-ROM | disk | FTP | other *** search
- /* =============
- * AEAHandler.cc
- * =============
- */
-
- //#define Debug_Throw
- //#define Debug_Signal
- #include "AEADebugging.h"
-
- // AEA
- #include "AEADescAppleEvent.hh"
- #include "AEAHandler.hh"
-
- AEEventHandlerUPP
- AEAHandler::sAEEventHandlerUPP = NewAEEventHandlerProc(GenericAEHandler);
-
- AEAHandler::AEAHandler(AEEventClass inEventClass, AEEventID inEventID)
- : mEventClass(inEventClass), mEventID(inEventID)
- {
- Install();
- }
-
- AEAHandler::~AEAHandler()
- {
- Remove();
- }
-
- void
- AEAHandler::Install()
- {
- OSErr err;
-
- err = ::AEInstallEventHandler(mEventClass, mEventID, sAEEventHandlerUPP, (long)this, false);
-
- ThrowIfOSErr_(err);
- }
-
- void
- AEAHandler::Remove()
- {
- OSErr err;
-
- err = ::AERemoveEventHandler(mEventClass, mEventID, sAEEventHandlerUPP, false);
-
- ThrowIfOSErr_(err);
- }
-
- void
- AEAHandler::MakeNoteToTouchParam(AEKeyword inParamKey)
- {
- mParamsToTouch.Append(inParamKey);
- }
-
- void
- AEAHandler::TouchParameters(const AEADescAppleEvent &inAppleEvent)
- {
- OSErr err;
- DescType actualType;
- Size actualSize;
- AEKeyword key;
-
- key = mParamsToTouch.FirstDatum();
- while (key != (AEKeyword)NULL) {
- inAppleEvent.GetParameter(key, typeWildCard,
- actualType, NULL, (Size)0, actualSize);
- key = mParamsToTouch.Successor(key); // not reliable for 2x keys
- }
- }
-
- void
- AEAHandler::HandleEvent(const AEADescAppleEvent &inAppleEvent, AEADescAppleEvent &outReply)
- {
- OSErr err;
- DescType actualType;
- Size actualSize;
-
- TouchParameters(inAppleEvent);
-
- try {
- //SetDebugThrow_(debugAction_Nothing); // this isn't nice
- inAppleEvent.GetAttribute(keyMissedKeywordAttr,
- typeWildCard, actualType, NULL, (Size)0, actualSize);
- //SetDebugThrow_(debugAction_Alert);
- // If we've gotten all the parameters, then the keyword will be missing,
- // and GetAttribute will throw errAEDescNotFound.
- // We only get to here if the keyword is present and we missed parameters.
- throw errAEParamMissed;
- } catch (ExceptionCode code) {
- //SetDebugThrow_(debugAction_Alert);
- if (code != errAEDescNotFound)
- throw code;
- }
- HandleEventSelf(inAppleEvent, outReply);
- }
-
- void
- AEAHandler::HandleEventSelf(const AEADescAppleEvent &, AEADescAppleEvent &)
- {
- ThrowOSErr_(errAEEventNotHandled);
- }
-
- /*
- * GenericAEHandler
- *
- * This function gets installed as an Apple event handler with AEInstallEventHandler
- * for each AEAHandler object that gets constructed.
- *
- * inRefCon is a pointer to the AEAHandler.
- */
-
- pascal OSErr
- AEAHandler::GenericAEHandler(const AppleEvent *inAppleEvent, AppleEvent *outReply, long int inRefCon)
- {
- // If we don't have the real handler, we can't do much.
- // We should probably signal an error.
- if (inRefCon == NULL) return errAEEventNotHandled;
-
- // Throwing out of a callback is probably bad. Don't do it.
- try {
- const AEADescAppleEvent appleEvent(*inAppleEvent);
- AEADescAppleEvent reply(*outReply);
-
- ((AEAHandler *)inRefCon)->HandleEvent(appleEvent, reply);
- } catch (ExceptionCode code) {
- return code;
- } catch (...) {
- //SignalPStr_("\pThis sucks!");
- return errAEEventNotHandled;
- }
-
- return noErr;
- }
-